<?php

namespace WenRuns\Laravel\Admin\Form\Field\Grid\Field;

use WenRuns\Laravel\Admin\Form\Field\Grid\Column;
use WenRuns\Laravel\Admin\Form\Field\Grid\Grid;
use WenRuns\Laravel\Admin\Form\Field\Grid\Field\Number;


/**
 * @method Input input($column, $label = '')
 * @method Select select($column, $label = '')
 * @method Number number($column, $label = '')
 * @method Textarea textarea($column, $label = '')
 * @method Radio radio($column, $label = '')
 * @method Checkbox checkbox($column, $label = '')
 * @method Expand expand($column, $label, $content)
 * @method Modal modal($column, $label = '', $content = null, $title = '')
 * @method Color color($column, $label = '')
 * @method Decimal decimal($column, $label = '')
 * @method Display display($column, $label = '')
 * @method Hidden hidden($column, $label = '')
 * @method Time time($column, $label = '')
 * @method Date date($column, $label = '')
 * @method DateTime datetime($column, $label = '')
 * @method PowerSwitch switch ($column, $label = '')
 * @method Image image($column, $label = '')
 * @method Html html($label, $html)
 * @method Group group($label, \Closure $closure)
 * @method Email email($column, $label)
 * @method Phone phone($column, $label)
 * @method Password password($column, $label)
 * @method Range range($column, $label)
 * @method Tab tab(\Closure $closure)
 */
class Expand extends Column
{
    protected $type = 'expand';

    protected $content = null;

    protected $html = '';

    protected $columns = [];

    /**
     * @param Grid $grid
     * @param $column
     * @param $label
     * @param $closure
     */
    public function __construct(Grid $grid, $column, $label, $closure)
    {
        parent::__construct($grid, $column, $label);
        /**
         * 情况1: $closure \Closure
         * 情况2: $closure ExpandGrid::class
         * 情况3: $closure html
         */
        if ($closure instanceof \Closure) {
            call_user_func($closure, $this);
        } else if (class_exists($closure)) {
            $this->content = $closure;
        } else {
            $this->html = $closure;
        }
    }


    /**
     * 子级grid
     * @param $column
     * @param \Closure $closure
     * @param $label
     * @return Grid
     */
    public function grid($column, \Closure $closure, $label = '')
    {
        $grid = new Grid($column, [$closure, $label], $this);
        $this->columns[] = $grid;
        call_user_func($closure, $grid);
        return $grid;
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \Exception
     */
    public function __call($name, $arguments)
    {
        return Grid::checkColumn($name, $arguments, function ($column, $parameters) {
            $instance = new $column($this->grid, ...$parameters);
            $this->columns[] = $instance;
            return $instance;
        });
    }


    public function render()
    {
        $res = parent::render();
        $res['html'] = $this->html;
        $res['content'] = $this->content;
        $res['children'] = $this->children();
        return $res;
    }

    /**
     * @return array
     */
    protected function children()
    {
        $columns = [];
        foreach ($this->columns as $column) {
            if ($column instanceof Grid) {
                $columns[] = [
                    'label' => $column->label(),
                    'type' => 'grid',
                    'name' => $column->column(),
                    'options' => $column->initOption(),
                ];
            } else {
                $columns[] = $column->render();
            }
        }
        return $columns;
    }


    /**
     * @return array|mixed|null
     */
    public function getValue()
    {
        $value = parent::getValue(); // TODO: Change the autogenerated stub
        if (!empty($value)) {
            foreach ($this->columns as $column) {
                /** @var Column $column */
                if ($column instanceof Grid) {
                    $subValue = $value[$column->column()] ?? null;
                    if (!empty($subValue)) {
                        $column->value($subValue);
                    }
                    $value[$column->column()] = $column->initOption(2);
                } else if ($column instanceof Group) {
                    $value = $column->setRow($value)->getValue();
                } else if (
                    $column->withClosure instanceof \Closure ||
                    $column instanceof Modal ||
                    $column instanceof Expand
                ) {
                    $value[$column->getName()] = $column->setRow($value)->getValue();
                }
            }
        }
        return $value;
    }


    public function columnWidth($width = 6)
    {
        return $this->config('columnWidth', $width);
    }


    public function duration($value)
    {
        return $this->config('duration', $value);
    }


    public function colspan($value = true)
    {
        return $this->config('expand', !$value);
    }
}
